/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  FileReader
 * 
 * Description of class:
 * 
 * Class to read files with UTF-8 encoding.
 * 
 */

package karel.lang.newParserTools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FileReader {
    
    private static final int maxLines = 500;

    public static String readFile(InputStream in) {
        String res = "";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(in,"UTF8"));
            String line;
            while ((line = br.readLine()) != null) {
                res += line + "\n";
            }
        } catch (IOException ex) {
            Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }
    
    public static String readFile(File file, JFrame frame) {
        String res = "";
        try {
            boolean q = true;
            int i = 0;
            Scanner in = new Scanner(new java.io.FileReader(file));
            while(in.hasNextLine()) {
                if(q && i++ > maxLines) {
                    q = false;
                    if(popTheQuestion(file, frame)) {
                        return null;
                    }
                }
                res += in.nextLine();
                if(in.hasNextLine()) {
                    res += "\n";
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileReader.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        
        return res;
    }
    
    private static boolean popTheQuestion(File file, JFrame frame) {
        int n = JOptionPane.showConfirmDialog(
        frame,
        "Skráin "+file.getName()+" er stærri en "+maxLines+" línur. Viltu opna hana?" ,
        "Spurning!",
        JOptionPane.YES_NO_OPTION);
        return n == 1;
    }
}
